﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using S.PurchaseAndSales.Domain.Enum;
using S.PurchaseAndSales.Domain.Model;
using S.PurchaseAndSales.Read.Api.Application.Command.PurchaseFormCommand;
using S.PurchaseAndSales.Read.Api.Dto;
using S.PurchaseAndSales.Read.Api.Application.Command.SheepSaleCommand;
using S.PurchaseAndSales.Domain.Dto;
using Authorization.Center.Controllers;
using Authorization.Center.SqlServer;
using Microsoft.AspNetCore.Authorization;

namespace S.PurchaseAndSales.Read.Api.Controllers
{
    /// <summary>
    /// 采购与销售--读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PurchaseAndSalesReadController : BaseController
    {
        private readonly IMediator mediator;

        public PurchaseAndSalesReadController(MySqlDbContext db, IMediator mediator) : base(db)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 测试Apollo是否可以使用
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult ApolloTest([FromServices] IConfiguration configuration, string key)
        {
            return Ok(configuration.GetValue<string>(key));
        }

        /// <summary>
        /// 读取羊只采购---摘要枚举表
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetSummaryEnum()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<SummaryEnum>())
            {
                list.Add(new
                {
                    name = item.ToString(),
                    value = item
                });
            }
            return Ok(list);
        }

        /// <summary>
        /// 显示采购信息
        /// </summary>
        [Authorize]
        [HttpPost]
        public async Task<Pager<GetPurchaseFormDto>> GetPurchaseFormModel(PurchaseFormGetPagerCommand command)
        {
            return await mediator.Send(command);
        }


        /// <summary>
        /// 雪花Id显示生成流水号
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetYitIdHelper()
        {
            return Ok("这是郭佳的服务");
            //return Ok(YitIdHelper.NextId());
        }

        /// <summary>
        /// 查看客户信息
        /// </summary>
        /// <param name="comm"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<IActionResult> GetCustomer(GetCustomerListCommand comm)
        {
            return Ok(await mediator.Send(comm));
        }

        /// <summary>
        /// 显示羊只销售信息
        /// </summary>
        /// <param name ="comm"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<IActionResult> GetSheepSale(GetSheepListCommand comm)
        {
            return Ok(await mediator.Send(comm));
        }

        /// <summary>
        /// 获取客户的信息(反填)
        /// </summary>
        /// <param name="comm"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<IActionResult> GetCustomById(GetCustomByIdCommand comm)
        {
            return Ok(await mediator.Send(comm));
        }


        /// <summary>
        /// 客户信息下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetClient()
        {
            return Ok(await mediator.Send(new GetClientListCommand()));
        }


        /// <summary>
        /// 供应商分页显示查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<Pager<GetVendorPageDto>> GetVendorPage(VendorGetPagerCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 供应商下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetSelectVenDor()
        {
            return Ok(await mediator.Send(new VendorGetSelectCommand()));
        }
        /// <summary>
        ///  供应商查看
        /// </summary>
        /// <param name="vendorId"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<GetVendorByIdDto> GetVendorById(int vendorId)
        {
            return await mediator.Send(new VendorGetByIdCommand() {
                VendorId = vendorId
            });
        }
        /// <summary>
        /// 获取可以售卖的羊只耳号
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetSalesEarTagSel()
        {
            SalesEarTagSelCommand command=new SalesEarTagSelCommand();
            return Ok(await mediator.Send(command));
        }
        /// <summary>
        /// 通过羊只耳号获取羊只详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<IActionResult> GetEarTagBySheepId(SheepDetailsCommand salesEarTagSelCommand)
        {
            return Ok(await mediator.Send(salesEarTagSelCommand));
        }
        [HttpPost]
        [Authorize]
        public async Task<IActionResult> GetFillBySheepId(SheepFillCommand salesEarTagSelCommand)
        {
            return Ok(await mediator.Send(salesEarTagSelCommand));
        }


        /// <summary>
        /// 采购表反填
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<PurchaseFormGetByIdDto> GetPurchaseFormGetById(int purchaseFormId)
        {
            return await mediator.Send(new PurchaseFormGetByIdCommand()
            {
                PurchaseFormId=purchaseFormId
            });
        }


    }
}
